package grib2handler.bufr.pass;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import grib2handler.util.FileUtil;
import org.apache.log4j.Logger;

import wmoBufr.BufrException;
import wmoBufr.BufrFile;
import wmoBufr.BufrUtil;

public class BufrPass {
	private final static Logger logger = Logger.getLogger(BufrPass.class);

	public static List<String> bulidArgsList(String OUT_PATH) {
		List<String> argsList = new ArrayList<String>();
		argsList = new ArrayList<String>();
		argsList.add("-debugTable");
		argsList.add("0");

		argsList.add("-debugData");
		argsList.add("0");

		argsList.add("-parseStage");
		argsList.add("data");

		argsList.add("-outFormat");
		argsList.add("xml");

		argsList.add("-outStyle");
		argsList.add("full");

		argsList.add("-validate");
		argsList.add("n");

		argsList.add("-force");
		argsList.add("n");

		argsList.add("-allowDups");
		argsList.add("n");

		argsList.add("-tabledir_13.0");
		argsList.add("tabledir");

		argsList.add("-outSpec");
		argsList.add(OUT_PATH);

		argsList.add("-outSpec");
		argsList.add("#inFile#");

//		argsList.add("-outSpec");
//		argsList.add("_#msgNum:03#");

		argsList.add("-outSpec");
		argsList.add(".xml");

		argsList.add("-report");
		argsList.add("report: #inFile# msg #msgNum:03# #year:04#-#month:02#-#day:02# #hour:02#:#minute:02# cat #category#");

		argsList.add("-report");
		argsList.add(" #value_0_08_021.1# #value_0_04_001.1#-#value_0_04_002.1:02#-#value_0_04_003.1:02# #value_0_04_004.1:02#");

		argsList.add("-report");
		argsList.add(":#value_0_04_005.1:02# #value_0_08_021.2# #value_0_04_001.2#-#value_0_04_002.2:02#-#value_0_04_003.2:02#");

		argsList.add("-report");
		argsList.add(" #value_0_04_004.2:02#:#value_0_04_005.2:02# height min: #value_0_07_002.1# max: #value_0_07_002.2#");
		return argsList;
	}

	public static void parseBufrFile(String inFilePath, String outPath) {
		logger.info("开始解析" + inFilePath);
		List<String> argsList = bulidArgsList(outPath);
		// 创建目录
		FileUtil.creatPath(outPath);
		argsList.add("-inFile");
		argsList.add(inFilePath);
		String[] args = new String[0];
		args = argsList.toArray(args);
		try {
			BufrFile.mainPgm(args);
			logger.info("解析完成" + inFilePath);
		} catch (BufrException exc) {
			logger.error("解析失败" + inFilePath + exc.getMessage());
			BufrUtil.prtlnexc("caught", exc);
		}
	}
	
	/**
	 * 获取目录下的bufr文件名
	 * @param path
	 * @return
	 */
	public static List<String> readPathBufrFiles(String path) {
		List<String> result = new ArrayList<String>();
		try {
			File file = new File(path);
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
			  String name = files[i].getName();
			  if(!files[i].isDirectory() && name.endsWith(".bufr")){
				  result.add(name);
			  }
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 测试下载后的文件是否正常
	 * @param args
	 */
	public static void main(String[] args) {
		String IN_PATH = "bufrdown"+ "/" + "2014111618" + "/";
		String OUT_PATH = "bufrxml"+File.separator;
		List<String> argsList = bulidArgsList(OUT_PATH);
		List<String> listFile = readPathBufrFiles(IN_PATH);
		// TODO Auto-generated method stub
		for (String string : listFile) {
			argsList.add("-inFile");
			argsList.add(IN_PATH + string);
		}
		args = argsList.toArray(args);
		try {
			BufrFile.mainPgm(args);
		} catch (BufrException exc) {
			BufrUtil.prtlnexc("caught", exc);
			System.exit(1);
		}
	}
}
